//
//  OssManager.m
//  AliyunOSSiOS
//
//  Created by 243280864@qq.com on 2021/2/16.
//
//#import <AliyunOSSiOS/OSSService.h>
#import "OssManager.h"

@implementation OssManager

- (NSString *)getPublicURLWithObjectName:(NSString *)objectName{
    NSString * publicURL = @"";

    // sign public url
    OSSTask *task = [_client presignPublicURLWithBucketName:_bucketName
                                    withObjectKey:objectName];
    if (!task.error) {
        publicURL = task.result;
    } else {
        NSLog(@"sign url error: %@", task.error);
    }
    return publicURL;
}

- (NSString *)getConstraiURLWithObjectName:(NSString *)objectName isSign:(BOOL)isSign bucketName:(NSString *)bucketName {
    _bucketName = bucketName;
    
    NSString * privateURL = @"";

    if (isSign) {
        OSSTask *task = [_client presignConstrainURLWithBucketName:bucketName withObjectKey:objectName withExpirationInterval:30 * 60];
        if (!task.error) {
            privateURL = task.result;
        } else {
            NSLog(@"sign url error: %@", task.error);
        }
    }else {
        privateURL = [self getPublicURLWithObjectName:objectName];
    }
    
    return privateURL;
}

//- (void)bindName:(NSString *)bucketName{
//    _bucketName = bucketName;
//    NSLog(@"bucketName:%@",bucketName);
//}
//OSSTask *
- (NSDictionary *)uploadFileByPath:(NSString *)filePath andName:(NSString *)objName bucketName:(NSString *)bucketName progress:(void (^)(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend))progress taskBlock:(id(^)(OSSTask *task))taskBlock{

    _bucketName = bucketName;
    NSLog(@"bucketName:%@",bucketName);

    OSSPutObjectRequest *put = [OSSPutObjectRequest new];
    put.bucketName = bucketName;
    if (!objName||objName.length == 0) {
        NSString *timeString = [self getCurrentTimestamp];
        NSLog(@"时间戳:%@",timeString);
        put.objectKey = timeString;
    }else {
        put.objectKey = objName;
    }
    put.uploadingFileURL = [NSURL fileURLWithPath:filePath];
    put.uploadProgress = progress;
    OSSTask *task = [_client putObject:put];
    [task continueWithBlock:taskBlock];
    
    
    return @{@"task":task, @"put":put};
}

- (void)initClient:(NSString *)endPoint andStsServer:(NSString *)stsServer andNeedLog:(BOOL)needLog andRetryTime:(int)retryTime andRequestTimeout:(int)requestTimeout andSocketTimeout:(int)socketTimeOut{
    id<OSSCredentialProvider> credential =[[OSSAuthCredentialProvider alloc] initWithAuthServerUrl:stsServer];
    OSSClientConfiguration *conf = [OSSClientConfiguration new];
    conf.maxRetryCount = retryTime;
    conf.timeoutIntervalForRequest = requestTimeout;
    conf.timeoutIntervalForResource = socketTimeOut;
    if(needLog){
        [OSSLog enableLog];
    }
    _client = [[OSSClient alloc] initWithEndpoint:endPoint credentialProvider:credential clientConfiguration:conf];
}

// 获取当前时间戳
- (NSString *)getCurrentTimestamp {
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0]; // 获取当前时间0秒后的时间
    NSTimeInterval time = [date timeIntervalSince1970]*1000;// *1000 是精确到毫秒(13位),不乘就是精确到秒(10位)
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}

@end

